﻿using RPAVIEW.util;
using RPAVIEW.views.EditPage.flows;
using SUNRPA.CONTROLS.IMPL.domain;
using System;
using System.Windows;
using System.Windows.Controls;

namespace RPAVIEW.views.HomePages {
    /// <summary>
    /// TimeTrigger.xaml 的交互逻辑
    /// </summary>
    public partial class TimeTrigger:BaseParamViewAction {
        public TimeTrigger() {
            InitializeComponent();
            Trigger<TimeTriggerEntry> param = WindowUtil.GetWindow<TriggerInfo>().DataContext as Trigger<TimeTriggerEntry>;
            if(param.triggerEntry.isTimeEnded ?? false) { 
                ShowDatePicker(null, null);
            } else { 
                HideDatePicker(null, null);
            }
        }

        private void MonthChanged(object sender, TextChangedEventArgs e) {
            if(Int16.TryParse(this.month.Text, out Int16 month)) {
                if(month <=0) { 
                    this.month.Text = "1";
                }else if(month > 12) { 
                    this.month.Text = "12";
                } 
            } else { 
                this.month.Text = "1";
            }
        }

        private void HourChanged(object sender, TextChangedEventArgs e) {
            int min = 0;
            int max = 23;
            switch(GetFreqChecked()) {
                case 1:
                    min = 1;
                    max = 24;
                    break;
                default:
                   break;
            }
            if(Int16.TryParse(this.hour.Text, out Int16 hour)) {
                if(hour <min) { 
                    this.hour.Text = min.ToString();
                }else if(hour > max) { 
                    this.hour.Text = max.ToString();
                } 
            } else { 
                this.month.Text = min.ToString();
            }
        }

        private void MinuteChanged(object sender, TextChangedEventArgs e) {
            int min = 0;
            int max = 59;
            switch(GetFreqChecked()) {
                case 0:
                    min = 1;
                    max = 59;
                    break;
                default:
                   break;
            }
            if(Int16.TryParse(this.hour.Text, out Int16 minute)) {
                if(minute < min) { 
                    this.minute.Text = min.ToString();
                }else if(minute > max) { 
                    this.minute.Text = max.ToString();
                } 
            } else { 
                this.minute.Text = min.ToString();
            }
        }
        
        private int GetFreqChecked() {
            if(this.minr.IsChecked ?? false) { 
                return 0;
            }
            if(this.hourr.IsChecked ?? false) { 
                return 1;
            }
            if(this.dayr.IsChecked ?? false) { 
                return 2;
            }
            if(this.weekr.IsChecked ?? false) { 
                return 3;
            }
            if(this.monr.IsChecked ?? false) { 
                return 4;
            }
            return 0;
        }

        private void FreqChanged(object sender, System.Windows.RoutedEventArgs e) {
            string name = (sender as RadioButton).Name;
            switch(name) {
                case "minr":
                    this.minuteRow.Height = GridLength.Auto;
                    this.monthRow.Height = new GridLength(0);
                    this.weekRow.Height = new GridLength(0);
                    this.hourRow.Height = new GridLength(0);
                    break;
                case "hourr":
                    this.minuteRow.Height = GridLength.Auto;
                    this.monthRow.Height = new GridLength(0);
                    this.weekRow.Height = new GridLength(0);
                    this.hourRow.Height = GridLength.Auto;;
                    break;
                case "dayr":
                    this.minuteRow.Height = GridLength.Auto;
                    this.monthRow.Height = new GridLength(0);
                    this.weekRow.Height = new GridLength(0);
                    this.hourRow.Height = GridLength.Auto;
                    break;
                case "weekr":
                    this.minuteRow.Height = GridLength.Auto;
                    this.monthRow.Height = new GridLength(0);
                    this.weekRow.Height = GridLength.Auto;
                    this.hourRow.Height = GridLength.Auto;
                    break;
                case "monr":
                    this.minuteRow.Height = GridLength.Auto;
                    this.monthRow.Height = GridLength.Auto;
                    this.weekRow.Height = GridLength.Auto;
                    this.hourRow.Height = GridLength.Auto;
                    break;
                default:
                    break;
            }
        }

        private void ShowDatePicker(object sender, RoutedEventArgs e) {
            this.datePickerRow.Height = GridLength.Auto;
        }

        private void HideDatePicker(object sender, RoutedEventArgs e) {
            this.datePickerRow.Height = new GridLength(0);
        }

        public override void UpdateSource() {
            if(this.minr.IsChecked ?? false) { 
                this.minr.GetBindingExpression(RadioButton.IsCheckedProperty).UpdateSource();
            }
            if(this.hourr.IsChecked ?? false) { 
                this.hourr.GetBindingExpression(RadioButton.IsCheckedProperty).UpdateSource();
            }
            if(this.dayr.IsChecked ?? false) { 
                this.dayr.GetBindingExpression(RadioButton.IsCheckedProperty).UpdateSource();
            }
            if(this.weekr.IsChecked ?? false) { 
                this.weekr.GetBindingExpression(RadioButton.IsCheckedProperty).UpdateSource();
            }
            if(this.monr.IsChecked ?? false) { 
                this.monr.GetBindingExpression(RadioButton.IsCheckedProperty).UpdateSource();
            }
            this.month.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            this.z1.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z2.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z3.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z4.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z5.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z6.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.z7.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.minute.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            this.hour.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            this.isTimeEnded.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.endDate.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
        }
    }
}
